/**
 * @param {number[]} heights
 * @return {number[]}
 */
// 单调栈怎么设计
// 从后开始,如果当前人比后面的人高,那么当前人会把后面的人挡住,其他人也再也看不到
// 因此维护一个单调减的单调栈即可.
var canSeePersonsCount = function (heights) {
    let ans = new Array(heights.length).fill(0)
    const stack = []
    for(let i = heights.length - 1; i >= 0; i--) {
        while(stack.length !== 0 && heights[stack[stack.length - 1]] < heights[i]) {
            stack.pop()
            ans[i]++
        }
        if(stack.length) {
            ans[i]++
        }
        stack.push(i)
    }
    return ans
};
// 两个循环，超时啦，傻逼暴力法
var canSeePersonsCount1 = function (heights) {
    let ans = new Array(heights.length).fill(0)
    for (let i = 0; i < heights.length - 1; i++) {
        tempMax = 0
        for (let j = i + 1; j < heights.length; j++) {
            if (j === i + 1) {
                if (heights[j] > heights[i]) {
                    ans[i] = 1
                    break
                } else {
                    tempMax = heights[j]
                    ans[i] = 1
                }
            } else {
                if (heights[j] > tempMax && heights[j] < heights[i]) {
                    tempMax = heights[j]
                    ans[i]++
                } else if (heights[j] > tempMax && heights[j] > heights[i]) {
                    ans[i]++
                    break
                }
            }
        }
    }
    return ans
};
// console.log(canSeePersonsCount([10, 6, 8, 5, 11, 9]));
// console.log(canSeePersonsCount([2,10,3,4,8]));
// console.log(canSeePersonsCount([4,3,2,1]));
console.log(canSeePersonsCount([11,19,12,15,14,18,7,1,8,9]));
// console.log(canSeePersonsCount1([5,1,2,3,10]));